package com.shengde.jinwawaforleader.model.myJob.classStatus;

import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.shengde.jinwawaforleader.R;
import com.shengde.jinwawaforleader.network.BasexUtilsProtocol;
import com.shengde.jinwawaforleader.network.protocolbean.ProCoursekDetialInfo;
import com.shengde.jinwawaforleader.network.protocolbean.ProHomeworkDetailInfo;
import com.shengde.jinwawaforleader.network.protocolbean.ProNoticeDetailInfo;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.net.URLDecoder;

/**
 *
 * 作业和课程的详情
 * 
 * 
 **/
public class AssignmentsAndCourseDetailsActivity extends Activity {
	private static final String TAG = "AssignmentsAndCourseDet";
	public WebView show_wv;
	public TextView tv_title,fill_add;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.assignmentsandcoursedetails);
		
		show_wv = (WebView) findViewById(R.id.webView1);
		tv_title = (TextView) findViewById(R.id.tv_title);
		fill_add = (TextView)findViewById(R.id.fill_add);
		fill_add.setVisibility(View.INVISIBLE);
		WebSettings settings = show_wv.getSettings();
		settings.setJavaScriptEnabled(true);
		settings.setSupportZoom(true);
		settings.setBuiltInZoomControls(true);
		settings.setDisplayZoomControls(false);
		settings.setUseWideViewPort(true);
		settings.setLoadWithOverviewMode(true);
		settings.setTextZoom(300);

		findViewById(R.id.tv_back).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
		String title = getIntent().getStringExtra("title");
		if (!TextUtils.isEmpty(title)){
			tv_title.setText(title);
		}
		String noticeId = getIntent().getStringExtra("noticeId");
		String type = getIntent().getStringExtra("type");
		Log.i(TAG, "onCreate: id = " + noticeId);
		switch (type) {
		    case "3":
				getNoticeDetailRequest(noticeId);
		        break;
		    case "4":
				getHomeworkDetailRequest(noticeId);
		        break;
		    case "5":
				getCoursekDetialRequest(noticeId);
		        break;
		}
	}

	/**
	 * 公告详情接口
	 * @param noticeId
     */
	public void getNoticeDetailRequest(String noticeId){
		RequestParams params = BasexUtilsProtocol.sendHttpRequest(this,"/teacher/NoticeDetail");
		params.addParameter("noticeid", noticeId);
		x.http().get(params, new Callback.CommonCallback<String>() {
			@Override
			public void onSuccess(String result) {
				ProNoticeDetailInfo info = new Gson().fromJson(result, ProNoticeDetailInfo.class);
				if (info != null && info.getCode() == 0) {
					String emocontent = "";
					try {
						emocontent = URLDecoder.decode(info.getDetail(), "UTF-8");
					} catch (Exception e) {
						// TODO: handle exception
					}
					loadNoticeDetailDataWithBaseURL(info.getDetail(), emocontent);
				}
			}

			@Override
			public void onError(Throwable ex, boolean isOnCallback) {
			}

			@Override
			public void onCancelled(CancelledException cex) {
			}

			@Override
			public void onFinished() {
				BasexUtilsProtocol.dialog.cancel();
			}
		});
	}

	/**
	 * 班级作业详情接口
	 * @param homworkid
     */
	public void getHomeworkDetailRequest(String homworkid){
		RequestParams params = BasexUtilsProtocol.sendHttpRequest(this,"/teacher/HomeworkDetail");
		params.addParameter("homworkid", homworkid);
		x.http().get(params, new Callback.CommonCallback<String>() {
			@Override
			public void onSuccess(String result) {
				ProHomeworkDetailInfo info = new Gson().fromJson(result, ProHomeworkDetailInfo.class);
				if (info != null && info.getCode() == 0) {
					String emocontent = "";
					try {
						emocontent = URLDecoder.decode(info.getDetail(), "UTF-8");
					} catch (Exception e) {
						// TODO: handle exception
					}
					loadNoticeDetailDataWithBaseURL(info.getDetail(), emocontent);
				}
			}

			@Override
			public void onError(Throwable ex, boolean isOnCallback) {
			}

			@Override
			public void onCancelled(CancelledException cex) {
			}

			@Override
			public void onFinished() {
				BasexUtilsProtocol.dialog.cancel();
			}
		});
	}

	public void getCoursekDetialRequest(String courseid){
		RequestParams params = BasexUtilsProtocol.sendHttpRequest(this,"/teacher/CoursekDetial");
		params.addParameter("courseid", courseid);
		x.http().get(params, new Callback.CommonCallback<String>() {
			@Override
			public void onSuccess(String result) {
				ProCoursekDetialInfo info = new Gson().fromJson(result, ProCoursekDetialInfo.class);
				if (info != null && info.getCode() == 0) {
					String emocontent = "";
					try {
						emocontent = URLDecoder.decode(info.getDetail(), "UTF-8");
					} catch (Exception e) {
						// TODO: handle exception
					}
					loadNoticeDetailDataWithBaseURL(info.getDetail(), emocontent);
				}
			}

			@Override
			public void onError(Throwable ex, boolean isOnCallback) {
			}

			@Override
			public void onCancelled(CancelledException cex) {
			}

			@Override
			public void onFinished() {
				BasexUtilsProtocol.dialog.cancel();
			}
		});
	}

	public void loadNoticeDetailDataWithBaseURL(String detail,String emocontent){
		if (TextUtils.isEmpty(emocontent)) {
			show_wv.loadDataWithBaseURL(null, detail, "text/html", "utf-8", null);
		} else {
			show_wv.loadDataWithBaseURL(null, emocontent, "text/html", "utf-8", null);
		}
	}

}